package com.ychs.dpmms.controller;

import com.ychs.dpmms.entity.Developer;
import com.ychs.dpmms.entity.Notice;
import com.ychs.dpmms.entity.Report;
import com.ychs.dpmms.entity.ResultMessage;
import com.ychs.dpmms.service.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/9/18
 */
@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource(name = "noticeService")
    IBaseService<Notice> service;

    //查询方法
    @GetMapping("/search")
    public ResultMessage search(Integer limit, Integer page, Integer toUser,Integer state) {
        Map<String, Object> conditions = new HashMap<>();
        logger.debug("enter notice search");
        if (toUser != null) {
            conditions.put("toUser", toUser);
        }
        if (state != null) {
            conditions.put("state", state);
        }

        int count = service.searchTotalNumber(conditions);

        if (limit != null && page != null) {
            conditions.put("begin", (page - 1) * limit);
            conditions.put("pageSize", limit);
        }
        logger.debug(conditions.toString());
        List<Notice> noticeList = service.search(conditions);
        logger.debug("noticeList=" + noticeList.toString());
        ResultMessage resultMessage = new ResultMessage(true, noticeList, "" + count);
        return resultMessage;
    }

    //修改方法
    @PostMapping("/modify")
    public ResultMessage modify(@RequestBody Notice notice) {
        logger.debug(notice.toString());
        logger.debug("enter notice modify");
        String msg = "更新成功";
        boolean result = false;
        logger.debug("report=" + notice);

        result = service.modify(notice);
        msg = result ? "更新成功" : "更新失败";
        logger.debug("report add result = " + result);
        ResultMessage resultMessage = new ResultMessage(result, notice, msg);
        return resultMessage;
    }
}
